function [K,Weights,name]=puttestshere5(F,width,height,numparams,corrs,showplots)
%close all
if(nargin<6)
    showplots=0;
end
Weights=[];
name='sum curves';
K=[width 0 width/2; 0 width height/2; 0 0 1];
numsl=100;
minimal=1;
usedFs=[];
for i=1:size(F,2)
    if(size(F{1,i},1)~=0)
        usedFs=[usedFs i];
    end
end


k=0;

k=k+1;params(k) = struct('name', 'focal','ymeasure','foc1' ,'xmeasure','foc2', 'yKrow', 1, 'yKcol', 1, 'xKrow',2, 'xKcol', 2);
k=k+1;params(k) = struct('name', 'oc','ymeasure','xc' ,'xmeasure','yc', 'yKrow', 1, 'yKcol', 3, 'xKrow',2, 'xKcol', 3);
k=k+1;params(k) = struct('name', 'focal','ymeasure','foc2' ,'xmeasure','foc1', 'yKrow', 2, 'yKcol', 2, 'xKrow',1, 'xKcol', 1);
k=k+1;params(k) = struct('name', 'oc','ymeasure','yc' ,'xmeasure','xc', 'yKrow', 2, 'yKcol', 3, 'xKrow',1, 'xKcol', 3);




for p=1:size(params,2)
    newusedFs=[];
    ci=1;
    bv=0;
    datapts=cell(size(usedFs,2),1);
    
    maxd=-100000000;
    bestf=1;
    bestindex=1;
    
    for i=1:size(usedFs,2)
        [sols,xs]= provideFamilySolution_matrixnorm(F{1,usedFs(1,i)},width,height,2,numsl,K,params(p).ymeasure);
        
        
        if(size(sols,1)>0)
            datapts{ci,1}(:,2)=sols;
            datapts{ci,1}(:,1)=xs;
            datapts{ci,1}(:,3)=ones(numsl,1)*usedFs(1,i);
            datapts{ci,1}(:,4)=zeros(numsl,1);
            newusedFs=[newusedFs usedFs(1,i)];
            ci=ci+1;
        end
        
    end
    datapts = datapts(~cellfun(@isempty, datapts));
    usedFs= newusedFs;
    
    
    for i=1:size(usedFs,2)
        for j=1:size(usedFs,2)
            if(j~=i)
                datapts{i,1}(:,4)=datapts{i,1}(:,4)+exp((-abs(datapts{i,1}(:,2)-datapts{j,1}(:,2)))/(width/10));
            end
        end
        [pks,locs] = findpeaks(datapts{i,1}(:,4));
        
        for q=1:size(pks,2)
            
            
            if(pks(1,q)>maxd && datapts{i,1}(locs(1,q),2)>datapts{i,1}(1,1) )
                maxd=pks(1,q);
                bestf=i;
                bestindex=locs(1,q);
                bv=datapts{i,1}(locs(1,q),1);
            end
        end
        
    end
    
  
    
    K(params(p).xKrow,params(p).xKcol)=bv;
    
    if(showplots==1)
        plotcurvegraphs(datapts,p,params,bestf, bestindex);
    end
    
    
end


end

function []=plotcurvegraphs(datapts,p,params,index,bestindex)
numfs=size(  datapts,1);
cVec = 'bgrcmykbgrcmykbgrcmykbgrcmyk';%, cVec = [cVec cVec];

label1= params(p).ymeasure;
label2= params(p).xmeasure;

figure, hold on
for i=1:numfs
    plot(datapts{i,1}(:,1),datapts{i,1}(:,2),cVec(1+mod(i,length(cVec))))
end
xlabel(label2);
ylabel(label1);
title(['clusters '  label1 ' versus '  label2]);
hold off

figure, hold on
for i=1:numfs
    plot(datapts{i,1}(:,1),datapts{i,1}(:,4),cVec(1+mod(i,length(cVec))))
end
hold off
xlabel(label2);
ylabel(label1);
title(['sum curves '  label1 ' versus '  label2]);


% figure
% plot(datapts{index,1}(:,1),(datapts{index,1}(:,4)/mean(datapts{index,1}(:,4)))*mean(datapts{index,1}(:,2)),'r',datapts{index,1}(:,1),datapts{index,1}(:,2),'b')
% legend('sum curve','solution family');
% 
% 
% xlabel(label2);
% ylabel(label1);
% title([' wining '  label1 ' versus '  label2 ' winning x at index ' num2str(index) ' -> ( ' num2str(datapts{index,1}(bestindex,1)) ' , '   num2str(datapts{index,1}(bestindex,2)) ' )' ]);

end
